package com.efast.cafe.framework.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.enums.ResultStatus;


public class GlobalExceptionAdvice {

	
    public static ResultBean defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ResultBean res = new ResultBean();
		
		res.setSuccess(false);
        if (e instanceof org.springframework.web.servlet.NoHandlerFoundException) {
        	res.setErrorCode(ResultStatus.NOT_FOUND.getCode());
        }else if(e instanceof ServiceException){
        	ServiceException sx = (ServiceException)e;
        	res.setErrorCode(sx.getCode());
        	res.setMessage(sx.getMessage());
        }else if(e instanceof CafeQueryException){
        	res.setErrorCode(ResultStatus.QUERY_ERROR.getCode());
        	res.setMessage("通用查询服务执行失败，请检查配置是否正确。");
        }else if(e instanceof BindException){
        	res.setErrorCode(ResultStatus.PARAM_ERROR.getCode());
        	res.setMessage(ResultStatus.PARAM_ERROR.getMessage());
        }else if(e instanceof DuplicateKeyException){
        	res.setErrorCode(ResultStatus.DATA_REPEAT_ER.getCode());
        	res.setMessage(ResultStatus.DATA_REPEAT_ER.getMessage());
        } else {
        	res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
        	res.setMessage(ResultStatus.SYS_ERROR.getMessage());
        }
        res.setObject("");
        return res;
    }
}
